﻿
using System ;
using System.Diagnostics ;
using System.Drawing ;


namespace MskEdit.Msk.Frontend {

    public class MskFieldGeometer
    {
        #region Private data
        
        private MskCellGeometry captionCellGeometry_ ;
        private MskCellGeometry valueCellGeometry_ ;
        private int  fieldMargin_ ;    // Margin outside field control

        
        #endregion

        #region Syntax sugar
        
        private int captionCellWidth() {
            return  captionCellGeometry_.CellSize.Width ;
        }
        
        private int captionCellHeight() {
            return  captionCellGeometry_.CellSize.Height ;
        }
        
        private int captionLabelPadding() {
            return  captionCellGeometry_.LabelPadding ;
        }
        
        private int valueCellWidth() {
            return  valueCellGeometry_.CellSize.Width ;
        }
        
        private int valueCellHeight() {
            return  valueCellGeometry_.CellSize.Height ;
        }
        
        private int valueLabelPadding() {
            return  valueCellGeometry_.LabelPadding ;
        }
        
        #endregion
        
        #region Private field geometry calculations
         
        private int fieldCellWidth() {           
            Debug.Assert( captionCellWidth() == valueCellWidth() ) ;
            int fieldCellWidth = ( captionCellWidth() + valueCellWidth() ) / 2 ;          
            return fieldCellWidth ;
        }
        
        
        private int fieldCellHeight() {
            int fieldCellHeight = Math.Max( captionCellHeight(), valueCellHeight() ) ; 
            return fieldCellHeight ;
        }
        
        // fieldLabelPadding() does not make sense.
        
        private int fieldWidth( int captionCellCount, int valueCellCount ) {
            int fieldWidth = captionWidth( captionCellCount ) + valueWidth( valueCellCount ) - captionLabelPadding() - valueLabelPadding() ;           
            return fieldWidth ;
        }
        
        
        private int fieldHeight() {
            int fieldHeight = fieldCellHeight() - fieldMargin_ * 2 ;
            return fieldHeight ;
        }


        private int fieldX( int cellLocationX /* base 1 */ ) {
            int x = fieldCellWidth() * ( cellLocationX - 1 /* to base 0 */ ) - captionLabelPadding() ;
            return x ;
        }
        
        
        private int fieldY( int cellLocationY /* base 1 */ ) {
            int y = fieldCellHeight() * ( cellLocationY - 1 /* to base 0 */ ) + fieldMargin_ ;
            return y ;
        }
        
        #endregion
        
        #region Private caption geometry calculations
        
        private int captionWidth( int cellCount ) {
            int captionWidth = fieldCellWidth() * cellCount + captionLabelPadding() * 2 ;
            return captionWidth ;
        }
        
        
        private int captionHeight() {
            int captionHeight = fieldHeight() ;
            return captionHeight ;
        }
        
        #region Private caption panel geometry calgulations
        
        private int captionClipPanelWidth( int cellCount ) {
            int captionClipPanelWidth = fieldCellWidth() * cellCount + captionLabelPadding() - valueLabelPadding() ;
            return captionClipPanelWidth ;
        }


        private int captionClipPanelHeight() {
            int captionClipPanelHeight = fieldHeight() ;
            return captionClipPanelHeight ;
        }
        
        #endregion
        
        #endregion
        
        #region Private value geometry calculations
        
        private int valueWidth( int cellCount ) {
            int valueWidth = fieldCellWidth() * cellCount + valueLabelPadding() * 2 ;
            return valueWidth ;
        }
        
        
        private int valueHeight() {
            int valueHeight = fieldHeight() ;          
            return valueHeight ;
        }
        
        
        private int valueX( int captionCellCount ) {
            int valueX = captionWidth( captionCellCount ) - captionLabelPadding() - valueLabelPadding() ;
            return valueX ;
        }
        
        
        private int valueY() {
            int valueY = 0 ;
            return valueY ;
        }
        
        #endregion

        #region Public foundations
        
        public MskFieldGeometer( MskCellGeometry captionCellGeometry, MskCellGeometry valueCellGeometry, int fieldMargin ) {
            captionCellGeometry_ = captionCellGeometry ;
            valueCellGeometry_   = valueCellGeometry ;
            fieldMargin_ = fieldMargin ;
        }
        
        #endregion
        
        #region Public geometry calculations        
                
        public Size getFieldCellSize() {
            Size fieldCellSize =  new Size( fieldCellWidth(), fieldCellHeight() ) ;
            return fieldCellSize ;
        }


        public Size getCaptionSize( int captionCellCount ) {
            Size captionSize = new Size( captionWidth( captionCellCount ), captionHeight() ) ;
            return captionSize ;
        }


        public Point getCaptionLocation() {
            return new Point( 0, 0 ) ;         
        }
        
        
        public Size getCaptionClipPanelSize( int captionCellCount ) {
            Size captionClipPanelSize = new Size( captionClipPanelWidth( captionCellCount ), captionClipPanelHeight() ) ;
            return captionClipPanelSize ;
        }
        
        
        public Point getCaptionClipPanelLocation() {
            return new Point( 0, 0 ) ;
        }
        
        
        public Size getValueSize( int valueTextLength ) {
            Size valueSize = new Size( valueWidth( valueTextLength ), valueHeight() ) ;
            return valueSize ;
        }
        
        
        public Point getValueLocation( int captionCellCount ) {
            Point valueLocation = new Point( valueX( captionCellCount ), valueY() ) ;         
            return valueLocation ;
        }
       
        
        public Size getFieldSize( int captionTextLength, int valueTextLength ) {
            Size fieldSize = new Size( fieldWidth( captionTextLength, valueTextLength ), fieldHeight() ) ;
            return fieldSize ;
        }
                  
        
        public Point getFieldLocation( Point cellLocation ) {
            Point location = new Point( fieldX( cellLocation.X ), fieldY( cellLocation.Y ) ) ;         
            return location ;
        }

        #endregion
    }
}
